home *** CD-ROM | disk | FTP | other *** search
/ PLAYymate for OS/2 / Playmate for OS2.iso / p4os2008 / ckrmoves.c < prev    next >
C/C++ Source or Header  |  1990-09-01  |  6KB  |  163 lines

  1. /*----------------------------------------------------------------------
  2.    CKRMOVES.C -- Ckm routines for determining valid moves, Version 0.40
  3.                  (c) 1990, Charles Petzold
  4.   ----------------------------------------------------------------------*/
  5.  
  6. #include <os2.h>
  7. #include "checkers.h"
  8. #include "ckrmoves.h"
  9.                               // Some handy constants
  10. #define B   pbrd->ulBlack
  11. #define W   pbrd->ulWhite
  12. #define K   pbrd->ulKing
  13. #define E   (~B & ~W)                   // empty squares
  14. #define MP  valnojmp[0][i].ulGrid       // valid no-jump moves (positive)
  15. #define MN  valnojmp[1][i].ulGrid       // valid no-jump moves (negative)
  16. #define IP  valnojmp[0][i].incr         // valid no-jump increments (pos)
  17. #define IN  valnojmp[1][i].incr         // valid no-jump increments (neg)
  18. #define JP  valjumps[0][i].ulGrid       // valid jumps (positive)
  19. #define JN  valjumps[1][i].ulGrid       // valid jumps (negative)
  20. #define IP1 valjumps[0][i].incr1        // valid jumps increment 1 (pos)
  21. #define IN1 valjumps[1][i].incr1        // valid jumps increment 1 (neg)
  22. #define IP2 valjumps[0][i].incr2        // valid jumps increment 2 (pos)
  23. #define IN2 valjumps[1][i].incr2        // valid jumps increment 2 (neg)
  24.  
  25. static struct                 // Valid No-Jump Moves
  26.      {
  27.      SHORT incr ;
  28.      ULONG ulGrid ;
  29.      }
  30.      valnojmp[2][3] = { 3, 0x00E0E0E0, 4, 0x0FFFFFFF, 5, 0x07070707,
  31.                         3, 0x07070700, 4, 0xFFFFFFF0, 5, 0xE0E0E0E0 } ;
  32.  
  33. static struct                 // Valid Jump Moves
  34.      {
  35.      SHORT incr1, incr2 ;
  36.      ULONG ulGrid ;
  37.      }
  38.      valjumps[2][4] = { 3, 4, 0x00E0E0E0, 4, 3, 0x000E0E0E,
  39.                         4, 5, 0x00707070, 5, 4, 0x00070707,
  40.                         3, 4, 0x07070700, 4, 3, 0x70707000,
  41.                         4, 5, 0x0E0E0E00, 5, 4, 0xE0E0E000 } ;
  42.  
  43.      /*---------------------------------------------------------------------
  44.         Obtain 32-bit ULONG of all pieces that can be moved without jumping
  45.        ---------------------------------------------------------------------*/
  46.  
  47. ULONG CkmQueryAllMoveablePieces (BOARD *pbrd, SHORT sColor)
  48.      {
  49.      SHORT i ;
  50.      ULONG S = 0 ;       // stands for "source"
  51.  
  52.      for (i = 0 ; i < 3 ; i++)
  53.           {
  54.           if (sColor == BLACK)
  55.                {
  56.                S |= (((B &     MP) << IP) & E) >> IP ;
  57.                S |= (((B & K & MN) >> IN) & E) << IN ;
  58.                }
  59.           else
  60.                {
  61.                S |= (((W &     MN) >> IN) & E) << IN ;
  62.                S |= (((W & K & MP) << IP) & E) >> IP ;
  63.                }
  64.           }
  65.      return S ;
  66.      }
  67.  
  68.      /*-------------------------------------------------------
  69.         Obtain 32-bit ULONG of all pieces that can make jumps
  70.        -------------------------------------------------------*/
  71.  
  72. ULONG CkmQueryAllJumpablePieces (BOARD *pbrd, SHORT sColor)
  73.      {
  74.      SHORT i ;
  75.      ULONG S = 0 ;
  76.  
  77.      for (i = 0 ; i < 4 ; i++)
  78.           {
  79.           if (sColor == BLACK)
  80.                {
  81.                S |= ((((B &     JP) <<  IP1       ) & W) >>  IP1       ) &
  82.                     ((((B &     JP) << (IP1 + IP2)) & E) >> (IP1 + IP2)) ;
  83.  
  84.                S |= ((((B & K & JN) >>  IN1       ) & W) <<  IN1       ) &
  85.                     ((((B & K & JN) >> (IN1 + IN2)) & E) << (IN1 + IN2)) ;
  86.                }
  87.           else
  88.                {
  89.                S |= ((((W &     JN) >>  IN1       ) & B) <<  IN1       ) &
  90.                     ((((W &     JN) >> (IN1 + IN2)) & E) << (IN1 + IN2)) ;
  91.  
  92.                S |= ((((W & K & JP) <<  IP1       ) & B) >>  IP1       ) &
  93.                     ((((W & K & JP) << (IP1 + IP2)) & E) >> (IP1 + IP2)) ;
  94.                }
  95.           }
  96.      return S ;
  97.      }
  98.  
  99.      /*-----------------------------------------------------------------
  100.         Obtain all destinations of a particular move-without-jump piece
  101.        -----------------------------------------------------------------*/
  102.  
  103. ULONG CkmQueryMoveDestinations (BOARD *pbrd, SHORT sColor, ULONG ulPiece)
  104.      {
  105.      SHORT i ;
  106.      ULONG P = ulPiece, D = 0 ;
  107.  
  108.      for (i = 0 ; i < 3 ; i++)
  109.           {
  110.           if (sColor == BLACK)
  111.                {
  112.                D |= ((P & B &     MP) << IP) & E ;
  113.                D |= ((P & B & K & MN) >> IN) & E ;
  114.                }
  115.           else
  116.                {
  117.                D |= ((P & W &     MN) >> IN) & E ;
  118.                D |= ((P & W & K & MP) << IP) & E ;
  119.                }
  120.           }
  121.      return D ;
  122.      }
  123.  
  124.      /*------------------------------------------------------
  125.         Obtain all destination of a particular jumping piece
  126.        ------------------------------------------------------*/
  127.  
  128. ULONG CkmQueryJumpDestinations (BOARD *pbrd, SHORT sColor, ULONG ulPiece)
  129.      {
  130.      SHORT i ;
  131.      ULONG P = ulPiece, D = 0 ;
  132.  
  133.      for (i = 0 ; i < 4 ; i++)
  134.           {
  135.           if (sColor == BLACK)
  136.                {
  137.                D |= ((((P & B &     JP) <<  IP1       ) & W) << IP2) &
  138.                     ((((P & B &     JP) << (IP1 + IP2)) & E)       ) ;
  139.  
  140.                D |= ((((P & B & K & JN) >>  IN1       ) & W) >> IN2) &
  141.                     ((((P & B & K & JN) >> (IN1 + IN2)) & E)       ) ;
  142.                }
  143.           else
  144.                {
  145.                D |= ((((P & W &     JN) >>  IN1       ) & B) >> IN2) &
  146.                     ((((P & W &     JN) >> (IN1 + IN2)) & E)       ) ;
  147.  
  148.                D |= ((((P & W & K & JP) <<  IP1       ) & B) << IP2) &
  149.                     ((((P & W & K & JP) << (IP1 + IP2)) & E)       ) ;
  150.                }
  151.           }
  152.      return D ;
  153.      }
  154.  
  155.      /*----------------------------------------------------------
  156.         Obtain index of a jumped piece based on jumper's indices
  157.        ----------------------------------------------------------*/
  158.  
  159. SHORT CkmQueryJumpedPiece (SHORT sBeg, SHORT sEnd)
  160.      {
  161.      return (sBeg + sEnd) / 2 + (sBeg & 4 ? 0 : 1) ;
  162.      }
  163.